/**
 * package.json里的循环依赖检测
 * const packageDeps = {
 *   'A': ['B', 'C'],
 *   'B': ['C'],
 *   'C': ['A'], // 循环依赖 C -> A -> C
 *   'D': ['E'],
 *   'E': ['D']  // 循环依赖 E -> D -> E
 * };
 */
function findCircularDependencies(dependencies){

    const graph = {};
    const visited = {};
    const recursionStack = {};
    const cycles = [];

    // 构建依赖图
    for (const [pkg, deps] of Object.entries(dependencies)) {
        graph[pkg] = deps || [];
    }
    
    
    function dfs(node, path) {
        if (recursionStack[node]) {
            const cycleStart = path.indexOf(node);
            cycles.push(path.slice(cycleStart));
            return;
        }
        if (visited[node]) return;

        visited[node] = true;
        recursionStack[node] = true;
        path.push(node);
        
        for (const neighbor of graph[node] || []) {
        dfs(neighbor, [...path]);
        }

        recursionStack[node] = false;
    }    

    for (const node of Object.keys(graph)) {
        if (!visited[node]) {
            dfs(node, []);
        }
    }

    return cycles;
}